//
//  HYOUUserView.m
//  BestGift
//
//  Created by 温晓宇 on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYOUUserView.h"
#import "HYOUUserTableViewCell.h"
@interface HYOUUserView ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)UILabel *name;
@property(nonatomic, retain)UIButton *logout;

@property(nonatomic, retain)UIImageView *back;


@end

@implementation HYOUUserView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews{
    
    
    self.back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
    [self.back setImage:[UIImage imageNamed:@"Me_ProfileBackground@2x.jpg"]];
    [self addSubview:self.back];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(WIDTH / 2 - 50, 300 / 2 - 25, 100, 100);
    self.button.layer.cornerRadius = 50;
    self.button.layer.masksToBounds = YES;
    [self addSubview:self.button];
    [self.button setTitle:@"登录" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.button.backgroundColor = [UIColor whiteColor];
    
    [self.button addTarget:self action:@selector(loginAciton:) forControlEvents:UIControlEventTouchUpInside];
    
//    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, 300 / 2 - 25, 100, 100)];
//    image.layer.cornerRadius = 50;
//    image.layer.masksToBounds = YES;
//    image.backgroundColor = [UIColor whiteColor];
//    [back addSubview:image];
    self.name = [[UILabel alloc] initWithFrame:CGRectMake(0, self.back.frame.size.height - 80, WIDTH, 50)];
    self.name.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.name];
    
    
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 44)];
    aView.backgroundColor = [UIColor clearColor];
    [self.back addSubview:aView];
    
//    self.logout = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.logout.frame = CGRectMake(20, 20, WIDTH - (WIDTH - 50), 44);
//    [self addSubview:self.logout];
//    [self.logout setTitle:@"注销" forState:UIControlStateNormal];
//    [self.logout addTarget:self action:@selector(logoutAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *setup = [UIButton buttonWithType:UIButtonTypeCustom];
    setup.frame = CGRectMake(WIDTH - 60, 20, WIDTH - (WIDTH - 50), 44);
    //    setup.backgroundColor = [UIColor whiteColor];
    [setup setImage:[UIImage imageNamed:@"iconfont-shezhi"] forState:UIControlStateNormal];
    [self addSubview:setup];
    
    [setup addTarget:self action:@selector(setupAction:) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mar -- 审核 收藏
    self.myTableView = [[HYOUBaseTableView alloc] initWithFrame:CGRectMake(0, self.back.frame.size.height, WIDTH, HEIGHT - 64 - 300) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self addSubview:self.myTableView];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    
    [self.myTableView registerClass:[HYOUUserTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HYOUUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    HYOUOneCellModel *model = [[HYOUOneCellModel alloc]init];
    model = [self.allArray objectAtIndex:indexPath.row];
    cell.home = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HYOUOneCellModel *model = [self.allArray objectAtIndex:indexPath.row];
    NSLog(@"%@",model.content_url);
    [self.delegate showCollecWeb:model.content_url];
}

/*************删除**************/
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    HYOUOneCellModel *model = [self.allArray objectAtIndex:indexPath.row];
    //先删数据库
    [[HYOUSingleton shareDataSingleton] deleteHomeCellTable:model.title];
    //后删cell
    [self.allArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}


-(void)loginAciton:(UIButton *)btn{
    [self.delegate loginView];
}
-(void)setupAction:(UIButton *)btn{
    [self.delegate pushSetupView];
}
-(void)setUrl:(NSString *)url{
    if (_url != url) {
        [_url release];
        _url = [url retain];
    }
    NSLog(@"%@",url);
    NSURL *image = [NSURL URLWithString:url];
    NSData *data = [NSData dataWithContentsOfURL:image];
    [self.button setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
    
}
-(void)setNameStr:(NSString *)nameStr{
    if (_nameStr != nameStr) {
        [_nameStr release];
        _nameStr = [nameStr retain];
    }
    self.name.text = self.nameStr;
}
//-(void)logoutAction:(UIButton *)btn{
//    [self.delegate logout];
//}

@end
